package cn.kiwipeach.blog.exception;

import cn.kiwipeach.blog.base.AjaxResponse;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 描述：重写系统404默认行为
 *
 * @author kiwipeach
 * @since 2.0
 */
@Controller
@RequestMapping("error")
public class Error404Controller {

    @RequestMapping(value = "404")
    @ResponseBody
    public AjaxResponse<Integer> errorResponse() {
        return AjaxResponse.fail("-PAGE_NOT_FOUND", "找不到页面");
    }

    @RequestMapping(value = "404", produces = MediaType.TEXT_HTML_VALUE)
    public String errorHtml(HttpServletRequest request) {
        // 404页面将会转发到系统默认首页，配合Vue的处理路由转发404问题
        return "forward:" + request.getSession().getServletContext().getContextPath();
    }
}
